#pragma once
#include <iostream>
#include <memory>
#include "singleton.hpp"
#include "dbng.hpp"
#include "sqlite.hpp"
#include <mutex>




namespace bz
{
	class sqlite_conn {

	public:
		sigletonclass(sqlite_conn)

		bool init(std::string path,std::string edition);

		static ormpp::dbng<ormpp::sqlite>*  get_conn();

		ormpp::dbng<ormpp::sqlite>* get();

		void return_back();

	private:
		sqlite_conn();
		ormpp::dbng<ormpp::sqlite>* db_conn_;
		std::condition_variable condition_;
		std::mutex mutex_;
		bool used_;
	};


	
	struct sqlite_guard {
		sqlite_guard(ormpp::dbng<ormpp::sqlite>* con) : conn_(con), is_unlock_(false){}
		~sqlite_guard() {
			unlock();
		}

		void unlock() {
			if (!is_unlock_) {
				sqlite_conn::instance()->return_back();
				is_unlock_ = true;
			}
			
		}
	private:
		ormpp::dbng<ormpp::sqlite>* conn_;
		bool is_unlock_;
	};

}